package com.uam.core.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author lishf
 * @description 线程池
 */
@Configuration
@ComponentScan("com.uam.core.scheduler")
@EnableAsync
public class ExecutorConfig implements AsyncConfigurer {

	@Value("${thread.pool.core.size}")
	private int coreSize = 0;

	@Value("${thread.pool.max.size}")
	private int maxSize = 0;

	@Value("${thread.pool.max.alive.seconds}")
	private int maxAlive = 0;

	@Value("${thread.pool.queue.size}")
	private int queueSize = 0;

	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(this.coreSize);
		taskExecutor.setMaxPoolSize(this.maxSize);
		taskExecutor.setKeepAliveSeconds(this.maxAlive);
		taskExecutor.setQueueCapacity(this.queueSize);
		taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		taskExecutor.initialize();
		return taskExecutor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}
}
